package org.ewaf.framework.dao.impl;

import java.util.List;

import org.ewaf.framework.dao.IBaseDao;
import org.ewaf.framework.lucene.DocumentBuilder;
import org.ewaf.framework.lucene.DocumentTag;
import org.ewaf.framework.model.IDataObject;
import org.ewaf.framework.model.ListModel;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;




public class BaseDaoImpl extends SqlMapClientDaoSupport implements IBaseDao {

	/**
	 * sql所属的namespace
	 */
	protected String namespace;
	
	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public void create(IDataObject data) {
		getSqlMapClientTemplate().insert(namespace + ".create", data);
		
		DocumentTag  tag =			data.getClass().getAnnotation(DocumentTag.class);
		
		if(tag!=null){
//			String clzName= tag.value();
			
//			DocumentBuilder instance = (DocumentBuilder) Class.forName(clzName).newInstance();
			
//			instance.buildDocument(  quo);

		}
		
		
	}
	
	public boolean update(IDataObject data) {
		return getSqlMapClientTemplate().update(namespace + ".update", data) == 1;
	}
	
	public boolean deleteById(Object id) {
		return getSqlMapClientTemplate().delete(namespace + ".deleteById", id) == 1;
	}
	
	public int countByCondition(ListModel listModel) {

		return (Integer) getSqlMapClientTemplate().queryForObject(namespace + ".countByCondition", listModel);
	}
	public int count() {
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace + ".count");
	}

	@SuppressWarnings("unchecked")
	public List<IDataObject> query(ListModel listModel) {
		return getSqlMapClientTemplate().queryForList(namespace + ".query", listModel);
	}
	
	public IDataObject getById(Object id) {
		return (IDataObject) getSqlMapClientTemplate().queryForObject(namespace + ".getById", id);
	}

	@SuppressWarnings("unchecked")
	public List<IDataObject> getByIds(Object[] ids) {
		return getSqlMapClientTemplate().queryForList(namespace + ".getByIds", ids);
	}
	
	@SuppressWarnings("unchecked")
	public List<IDataObject> getByIdList(List<?> idList) {
		return getSqlMapClientTemplate().queryForList(namespace + ".getByIds", idList);
	}
	
}
